package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Checklist;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VChecklist;
import com.jxc.service.ChecklistServiceI;

@Service("checklistService")
public class ChecklistServiceImpl implements ChecklistServiceI {

	@Autowired
	private BaseDaoI<Checklist> checklistDao;

	@Override
	public List<VChecklist> combobox(VChecklist vcl) {
		return this.changeModel(this.find(vcl));
	}

	@Override
	public DataGrid datagrid(VChecklist vcl) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vcl)));
		dg.setTotal(this.total(vcl));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Checklist cl = checklistDao.get(Checklist.class, Long.parseLong(id.trim()));
					if (cl != null) {
						checklistDao.delete(cl);
					}
				}
			}
		}
	}

	@Override
	public void reset(VChecklist vcl) {
		Checklist cl = checklistDao.get(Checklist.class, vcl.getId());
		cl.setIsCheckOver(false);
		checklistDao.update(cl);
	}

	@Override
	public Long save(VChecklist vcl) {
		Checklist cl = new Checklist();
		BeanUtils.copyProperties(vcl, cl);
		if (vcl.getUid() != null) {
			cl.setUser(new User(vcl.getUid()));
		}
		if (vcl.getRid() != null) {
			cl.setRepertory(new Repertory(vcl.getRid()));
		}
		this.checklistDao.save(cl);
		return cl.getId();
	}

	@Override
	public void update(VChecklist vcl) {
		Checklist cl = checklistDao.get(Checklist.class, vcl.getId());
		BeanUtils.copyProperties(vcl, cl, new String[] { "id" });
		if (vcl.getUid() != null) {
			cl.setUser(new User(vcl.getUid()));
		}
		if (vcl.getRid() != null) {
			cl.setRepertory(new Repertory(vcl.getRid()));
		}
		this.checklistDao.update(cl);
	}

	private List<VChecklist> changeModel(List<Checklist> cls) {
		List<VChecklist> vcls = new ArrayList<VChecklist>();
		if (cls != null && cls.size() > 0) {
			for (Checklist cl : cls) {
				VChecklist vcl = new VChecklist();
				BeanUtils.copyProperties(cl, vcl);
				if (cl.getUser() != null) {
					User user = cl.getUser();
					vcl.setUid(user.getId());
					vcl.setUname(user.getUsername());
				}
				if (cl.getRepertory() != null) {
					Repertory repertory = cl.getRepertory();
					vcl.setRid(repertory.getId());
					vcl.setRname(repertory.getName());
				}
				vcls.add(vcl);
			}
		}
		return vcls;
	}

	private List<Checklist> find(VChecklist vcl) {
		String hql = "from Checklist t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vcl, hql, values);

		if (vcl.getSort() != null && vcl.getOrder() != null) {
			hql += " order by " + vcl.getSort() + " " + vcl.getOrder();
		}
		return checklistDao.find(hql, values, vcl.getPage(), vcl.getRows());
	}

	private Long total(VChecklist vcl) {
		String hql = "select count(*) from Checklist t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vcl, hql, values);
		return checklistDao.count(hql, values);
	}

	private String addWhere(VChecklist vcl, String hql, List<Object> values) {
		if (vcl.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vcl.getId());
		}
		if (vcl.getBeginDate() != null) {
			hql += " and t.date >= ? ";
			values.add(vcl.getBeginDate());
		}
		if (vcl.getEndDate() != null) {
			hql += " and t.date <= ? ";
			values.add(vcl.getEndDate());
		}
		if (vcl.getUid() != null) {
			hql += " and t.user.id = ? ";
			values.add(vcl.getUid());
		}
		if (vcl.getRid() != null) {
			hql += " and t.repertory.id = ? ";
			values.add(vcl.getRid());
		}
		return hql;
	}

}
